//
//  ServiewDeatilViewController.m
//  Jingjing
//
//  Created by Stl on 15/11/24.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "ServiewDeatilViewController.h"

@interface ServiewDeatilViewController ()<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView* webVeiw;
@property (nonatomic, strong) UIScrollView *backgroundScrollView;

@property (strong, nonatomic)UIView *noDataView;
@property(nonatomic,strong)UIView *noNet;





@end

@implementation ServiewDeatilViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor hexStringToColor:@"#f3f3f3"];
    
//    self.view.backgroundColor = [UIColor redColor];
    [self httpRequest];
    
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:self.titleName];
    [self.view addSubview:topView];
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 20.0f, 44.0f, 44.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
    
    [self.view addSubview:self.noDataView];
    [self.view addSubview:self.noNet];

    
    
    _backgroundScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0f, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT - 64.0f)];
    [self.view addSubview:_backgroundScrollView];
    
    
    _webVeiw = [[UIWebView alloc]initWithFrame:CGRectMake(5.0f,  5.0f, SCREEN_WIDTH - 10.0f, SCREEN_HEIGHT - 5.0f-64.0f)];
    _webVeiw.delegate = self;
    _webVeiw.scrollView.showsHorizontalScrollIndicator = FALSE;
    _webVeiw.scrollView.showsVerticalScrollIndicator = FALSE;
    _webVeiw.backgroundColor = [UIColor redColor];
    _webVeiw.scrollView.scrollEnabled = FALSE;
    _webVeiw.hidden = FALSE;
    [_backgroundScrollView addSubview:_webVeiw];
    [_backgroundScrollView addSubview:self.noDataView];
    [_backgroundScrollView addSubview:self.noNet];
    
    
    
    

    
    
}

#pragma mark - 没网提示
-(UIView *)noNet
{
    if (!_noNet) {
        _noNet = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 235)];
        _noNet.center = CGPointMake(self.view.center.x, self.view.center.y - 32);
        _noNet.hidden = TRUE;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 200, 200);
        [button setImage:[UIImage imageNamed:@"img_nonetwork"] forState:UIControlStateNormal];
        //    [button setTitle:@"暂无数据\n点击重新搜索" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAgainTapped:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 - 44, 200, 44)];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:@"加载失败\n请检查手机是否联网"];
        
        [aString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],
                                 NSForegroundColorAttributeName:[UIColor colorWithWhite:0.098 alpha:1.000],
                                 } range:NSMakeRange(0, 5)];
        
        [aString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],
                                 NSForegroundColorAttributeName:[UIColor colorWithWhite:0.702 alpha:1.000],
                                 } range:NSMakeRange(5, 9)];
        [label setAttributedText:aString];
        [_noNet addSubview:label];
        [_noNet addSubview:button];
        
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(60, 210, 80, 25);
        btn.backgroundColor = [UIColor colorWithWhite:0.200 alpha:1.000];
        btn.layer.cornerRadius = 6;
        btn.layer.masksToBounds = YES;
        [btn setTitle:@"重新加载" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(buttonAgainTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_noNet addSubview:btn];
    }
    return _noNet;
}

-(UIView *)noDataView{
    if (!_noDataView) {
        
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
        _noDataView.center = CGPointMake(self.view.center.x, self.view.center.y - 32);
        _noDataView.hidden = TRUE;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 200, 200);
        [button setImage:[UIImage imageNamed:@"img_nodata"] forState:UIControlStateNormal];
        //    [button setTitle:@"暂无数据\n点击重新搜索" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAgainTapped:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 - 44, 200, 44)];
        label.text = @"暂无数据\n点击重新搜索";
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:14.0f];
        
        label.textColor = [UIColor colorWithWhite:0.000 alpha:0.540];
        [_noDataView addSubview:label];
        [_noDataView addSubview:button];
        
    }
    
    return _noDataView;
}



- (void)httpRequest
{
    
    self.noNet.hidden = TRUE;
    self.noDataView.hidden = TRUE;

    [[AnimationIndicator sharedIndicator] startAnimationWithLoadText:@"加载中..."];
    [AppGlobalClass PostDataWithNSstring:@"/jijiapp/app/activities/getJingServiceDetail" withDictionary:@{@"jingServiceId":self.nameId} success:^(NSDictionary *dictionary) {
        
        
        
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            NSMutableArray *listArray = [[[dictionary objectForKey:@"responseData"]firstObject] objectForKey:@"rows"];
            
            if (listArray.count!= 0) {
                
                NSDictionary* dict = [listArray objectAtIndex:0];
                NSString* contString = [dict objectForKey:@"content"];
                NSLog(@"________%@",contString);
                _webVeiw.hidden = FALSE;
                [_webVeiw loadHTMLString:contString baseURL:nil];
            }
            
            
            
            

            
            
            
        }else
        {
            NSString * str = [dictionary objectForKey:@"message"];
            
            ALERT_VIEW(str);
            
            self.noNet.hidden = FALSE;
            self.noDataView.hidden = TRUE;

            
        }
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
        
    } failure:^(NSError *error) {
        
        
        self.noDataView.hidden = FALSE;
        self.noNet.hidden = TRUE;

        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    } networkerror:^(NSString *networkerror) {
        
        
        self.noDataView.hidden = TRUE;
        self.noNet.hidden = FALSE;

        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    }];
    
}


-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
//   CGRect frame = webView.frame;
//    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
//   frame.size = fittingSize;
//    webView.frame = frame;
//    self.view.frame = frame;
//    
//    
//    
//    float totalHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
//
//    
//    NSLog(@"_______%f",webView.frame.size.height);
//    
//
//    NSLog(@"_______**********%f",totalHeight);
    
    
    float totalHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
    
    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, totalHeight);

    _backgroundScrollView.contentSize = CGSizeMake(_backgroundScrollView.frame.size.width, webView.frame.origin.y + totalHeight );
    
}

-(void)buttonAgainTapped:(id)sender
{
    [self httpRequest];
}

- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
